package com.example.tqyb;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.example.weather.bean.WeatherBean;
import com.example.weather.bean.realtime;
import com.example.weather.bean.result;
import com.example.weather.util.NetUtil;
import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    private AdapterView mSpinner;
    private ArrayAdapter<String>mSpAdapter;
    private String[] mCities;

    private TextView tvWeather,tvTem,tvTemLowHigh,tvWin,tvAir;
    private ImageView ivWeather;
    private RecyclerView rlvFutureWeather;

    private Handler mHandler=new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what==0){
               String weather=(String)msg.obj;
               Log.d("fan","----主线中收到了天气数据-weather---"+weather);

                Gson gson=new Gson();
                WeatherBean weatherBean = gson.fromJson(weather, WeatherBean.class);
                Log.d("fan","--解析后的数据--"+weatherBean);

                updateUifoWeather(weatherBean);

            }
        }
    };

    private void updateUifoWeather(WeatherBean weatherBean) {
        if (weatherBean == null){
            return;
        }
        String timeNow = getNowDay("yyyy-MM-dd");
        result result = weatherBean.getResult();
        realtime realtime = result.getRealtime();

        tvTem.setText(realtime.getTemperature()+"°C");
        tvWeather.setText(realtime.getInfo()+timeNow);
        tvTemLowHigh.setText("湿度"+":"+realtime.getHumidity());
        tvWin.setText(realtime.getDirect()+":"+realtime.getPower());
        ivWeather.setImageResource(+getImgResOfWeather(realtime.getWid()));
    }

    private int getImgResOfWeather(String wid){
        int result=0;
        switch (wid){
            case "00":
                result=R.drawable.biz_plugin_weather_qing;
                break;
            case "01":
            case "19":
                result=R.drawable.biz_plugin_weather_duoyun;
                break;
            case "02":
                result=R.drawable.biz_plugin_weather_yin;
                break;
            case "03":
                result=R.drawable.biz_plugin_weather_zhenyu;
                break;
            case "04":
                result=R.drawable.biz_plugin_weather_leizhenyu;
                break;
            case "05":
                result=R.drawable.biz_plugin_weather_leizhenyubingbao;
                break;
            case "06":
                result=R.drawable.biz_plugin_weather_yujiaxue;
                break;
            case "07":
            case "21":
                result=R.drawable.biz_plugin_weather_xiaoyu;
                break;
            case "08":
                result=R.drawable.biz_plugin_weather_zhongyu;
                break;
            case "09":
                result=R.drawable.biz_plugin_weather_dayu;
                break;
            case "10":
                result=R.drawable.biz_plugin_weather_baoyu;
                break;
            case "11":
                result=R.drawable.biz_plugin_weather_dabaoyu;
                break;
            case "12":
                result=R.drawable.biz_plugin_weather_tedabaoyu;
                break;
            case "13":
                result=R.drawable.biz_plugin_weather_zhenxue;
                break;
            case "14":
                result=R.drawable.biz_plugin_weather_xiaoxue;
                break;
            case "15":
                result=R.drawable.biz_plugin_weather_zhongxue;
                break;
            case "16":
                result=R.drawable.biz_plugin_weather_daxue;
                break;
            case "17":
                result=R.drawable.biz_plugin_weather_baoxue;
                break;
            case "18":
                result=R.drawable.biz_plugin_weather_wu;
                break;
            case "20":
                result=R.drawable.biz_plugin_weather_shachenbao;
                break;
            case "22":
                result=R.drawable.biz_plugin_weather_zhongyu;
                break;
            case "23":
                result=R.drawable.biz_plugin_weather_baoyu;
                break;
            case "24":
                result=R.drawable.biz_plugin_weather_dabaoyu;
                break;
            case "25":
                result=R.drawable.biz_plugin_weather_tedabaoyu;
                break;
            case "26":
                result=R.drawable.biz_plugin_weather_zhongxue;
                break;
            case "27":
                result=R.drawable.biz_plugin_weather_daxue;
                break;
            case "28":
                result=R.drawable.biz_plugin_weather_baoxue;
                break;
            default:
                result=R.drawable.ic_launcher_foreground;
                break;
        }
        return result;
    }

    private String getNowDay(String timeFormat) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeFormat);
        String dateString = simpleDateFormat.format(new Date()); //将给定的 Date 格式化为日期/时间字符串
        return dateString;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

    }

    private void initView(){
        mSpinner=findViewById(R.id.sp_city);
        mCities=getResources().getStringArray(R.array.cities);
        mSpAdapter=new ArrayAdapter<>(this,R.layout.sp_item_layout,mCities);
        mSpinner.setAdapter(mSpAdapter);
        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                String selectedCity=mCities[position];
                getWeatherOfCity(selectedCity);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        tvWeather=findViewById(R.id.tv_weather);
        tvAir=findViewById(R.id.tv_air);
        tvTem=findViewById(R.id.tv_tem);
        tvTemLowHigh=findViewById(R.id.tv_tem_low_high);
        tvWin=findViewById(R.id.tv_win);
        ivWeather=findViewById(R.id.vi_weather);
        rlvFutureWeather=findViewById(R.id.rlv_future_weather);
    }

    private void getWeatherOfCity(String selectedCity) {
        //开启子线程、请求网络
        new Thread(new Runnable() {
            @Override
            public void run() {
            //请求网络
                String weatherOfCity = NetUtil.getWeatherOfCity(selectedCity);
            //使用handler将数据传递主线程
                Message message=Message.obtain();
                message.what=0;
                message.obj=weatherOfCity;
                mHandler.sendMessage(message);
            }
        }).start();
    }
}